-
Notifications
You must be signed in to change notification settings - Fork 34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
k8s.contrib.crd - Fix class name collision for colliding nested properties. #77
base: main
Are you sure you want to change the base?
Conversation
…ml adding support for colliding nested sub-objects.
…ng properties (objects, list). This change will result in: "spec.foo.bar": - current: classes ["Spec", "Foo", "Bar"] - change: classes ["Spec", "SpecFoo", "SpecFooBar" This change is suggested by https://github.com/Avarei in apple#40 (comment)
31f611d
to
9916a71
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for looking into this!
The issue here is really that there's a bug in the conflict detection logic; the fix is:
diff --git a/packages/k8s.contrib.crd/internal/ModuleGenerator.pkl b/packages/k8s.contrib.crd/internal/ModuleGenerator.pkl
index 4662bfb..87da6b7 100644
--- a/packages/k8s.contrib.crd/internal/ModuleGenerator.pkl
+++ b/packages/k8s.contrib.crd/internal/ModuleGenerator.pkl
@@ -281,18 +281,19 @@ function isClassLike(schema: JsonSchema.Schema): Boolean =
/// Try to use the parent property's name as part of the class name in case of conflict.
/// If already at the root, add a number at the end.
local function determineTypeName(path: List<String>, candidateName: String, existingTypeNames: Set<Type>, index: Int): Type =
- if (existingTypeNames.contains(utils.pascalCase(candidateName)))
- if (path.isEmpty)
- determineTypeName(path, candidateName + index.toString(), existingTypeNames, index + 1)
+ let (candidate = utils.pascalCase(candidateName))
+ if (existingTypeNames.findOrNull((it) -> it.name == candidate) != null)
+ if (path.isEmpty)
+ determineTypeName(path, candidateName + index.toString(), existingTypeNames, index + 1)
+ else
+ determineTypeName(
+ path.dropLast(1),
+ utils.pascalCase(path.last.capitalize()) + candidate,
+ existingTypeNames,
+ index
+ )
else
- determineTypeName(
- path.dropLast(1),
- utils.pascalCase(path.last.capitalize()) + utils.pascalCase(candidateName),
- existingTypeNames,
- index
- )
- else
- new { name = utils.pascalCase(candidateName); moduleName = module.moduleName }
+ new { name = candidate; moduleName = module.moduleName }
/// The schemas that should be rendered as classes.
///
Can you apply this diff, undo your current change? It'd be good to not incur a breaking change when we don't really need it.
@bioball, (edited, after re-reading your comment). |
With the diff, i see the following:
|
I suspect the intent was:
Instead of:
Which results in:
|
@bioball , any updates on this? |
Yeah, your suggestion looks right. Happy to accept that change! Apologies on the late response here! |
Fix colliding class names when rendering CRD with duplicated nested properties.
With this PR, the generated *pkl file changes the file naming convention for "spec.compute.field":
This change was suggested in #40 (comment)
Closes: #40
Currently, cards.yaml does not handle CRDs containing duplicated nested objects/lists resulting in the pkl file/module with duplicate class names. For example, if input CRD contains duplicated nested properties as in snipped below:
We can observe colliding class definitions results in generated pkl file, such as:
Similarly, with list types: